﻿
Imports System.Net
Imports System.Net.Sockets
Imports udpTrans

Public Class udptalk
    Public Const defaultHoldTime As Int64 = udpserver.defaultHoldTime
    Public holdtime As Int64 = defaultHoldTime
    Private handle As udpserver.talkHandle = Nothing
    Private client As UdpClient = Nothing

    Public Sub New()
        handle = New udpserver.talkHandle()
        client = New UdpClient()
    End Sub

    Public Property remoteAddress() As IPEndPoint
        Get
            Return handle.remoteAddress
        End Get
        Set(ByVal value As IPEndPoint)
            handle.remoteAddress = New IPEndPoint(value.Address, value.Port)
        End Set
    End Property

    Public Function receiveData() As nettransData
        Return handle.receive
    End Function

    Public Function sendData() As nettransData
        Return handle.send
    End Function

    Public Function talk() As Boolean
        Dim startTime As Int64 = 0
        startTime = Now.milliseconds()
        If udpTrans.send(remoteAddress(), sendData(), holdtime) Then
            If udpTrans.receive(handle.remoteAddress, handle.receive, holdtime) Then
                Return True
            Else
                raiseError(errorHandle.errorType.system, "cannot receive response from server ",
                           Convert.ToString(remoteAddress()))
                Return False
            End If
        Else
            raiseError(errorHandle.errorType.system, "cannot send data to server ", Convert.ToString(remoteAddress()))
            Return False
        End If
    End Function
End Class
